import com.hnsp.edge.EdgeGrpc;
import com.hnsp.edge.GrpcBuilder;
import service.DeviceGrpcImpl;

/**
 * @ClassName Start
 * @Description
 * @Author wang.shouyan
 * @Date 2023/6/7 20:34
 **/
public class Start {

    /**
     * appid, 请在http://192.168.10.110:30808/platform/#/iot/device/table中获取.
     */
    private static final String appId = "1664564130921385986";

    /**
     * 设备SN号,请在http://192.168.10.110:30808/platform/#/iot/device/table?productId=1664564130938163202中获取.
     */
    private static final String sn = "SN2";

    /**
     * 协议版本, 默认 2.0.
     */
    private static final String version = "2.0";

    /**
     * 边缘端ip, 该测试环境默认: 192.168.10.112.
     */
    private static final String EDGE_HOST = "192.168.10.112";
    /**
     * 边缘端设备端口号, 一般固定的.
     */
    private static final int EDGE_PORT = 8083;
    /**
     * 边缘端ip, 当前程序运行所在的ip, 请该用自己电脑的IP, 要保证能和服务器互ping.
     */
    private static String TARGET_HOST = "192.168.51.201";
    /**
     * app监听地址,用于启动本地服务监听.
     */
    private static final int APP_PORT = 8089;
 
    public static void main(String[] args) throws Exception {
        // 通过构造器, 创建一个grpc客户端
        GrpcBuilder grpcBuilder = new GrpcBuilder();
        grpcBuilder.setAppId(appId);
        grpcBuilder.setSn(sn);
        grpcBuilder.setTargetHost(EDGE_HOST);
        grpcBuilder.setTargetPort(EDGE_PORT);
        grpcBuilder.setLocalHost(TARGET_HOST);
        grpcBuilder.setLocalPort(APP_PORT);

        // 获取grpc客户端
        EdgeGrpc edge = grpcBuilder.builde();
        // grpc客户端初始化
        edge.init();
        // 注册设备(注册该app)
        edge.registerDevice();
        // 上报状态
        edge.uploadStatus("2");
        // 启动app监听服务,用户接受云端的事件属性下发
        edge.startLocalService(new DeviceGrpcImpl());

        while (true){
            // 上报数据
            edge.uploadProperty("{\"data2\":\"1234\",\"data2\":\"5678\"}");
            // 上报属性
            edge.uploadEvent("{\"bs1\":\"value1\",\"bs2\":\"value2\"}");
            Thread.sleep(5000);
        }
    }

}
